package com.isoft.game.sys.user.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.isoft.game.common.model.PersistModel;
import com.isoft.game.common.service.BaseService;
import com.isoft.game.common.util.EncoderByMd5;
import com.isoft.game.common.util.FamilyLogger;
import com.isoft.game.common.util.UserUtils;
import com.isoft.game.sys.user.dao.UserRepository;
import com.isoft.game.sys.user.entity.User;
import com.isoft.game.sys.user.service.UserService;

@Service
@Transactional(readOnly = true)
public class UserServiceImpl extends BaseService implements UserService {

    @Resource
    public UserRepository userRepository;

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
	@Override
	public PersistModel persistUser(User user) {
    	try {
			String newstr = EncoderByMd5.encoderByMd5(user.getPassword());
			user.setPassword(newstr);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		user.setCommonBusinessId();
		UserUtils.setCurrentPersistOperation(user, user);
		int line = userRepository.insertSelective(user);
		FamilyLogger.sysInfo("当前登陆人"+user.getBusinessId(),"当前登陆人" + user.getUsername() + "新增了ID为"+user.getBusinessId()+"的用户");
		return new PersistModel(line);
	}

	@Override
	public User queryUserByName(User user) {
		User result= userRepository.queryUserByName(user);
		if(null != result){
		    return result;
		}
			return null;	
	}
}
